<?php


namespace app\admin\controller;

use app\common\controller\Adminbase;
use think\Db;
class Fnurselevel extends Adminbase
{
    public function index(){
        return '开发中。。。。';
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $business_where[] = ['is_del', '=', 0];
            $business_where[] = ['status', 'in', [1,2]];
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
            }
            $_list = Db::name('fx')
                ->page($page, $limit)
                ->where($business_where)
                ->order('create_time desc')
                ->select();
            foreach ($_list as $k=>$v){
                $member=Db::name('member')->find($v['member_id']);
                $_list[$k]['nickname']=$member['nickname'];
                $_list[$k]['avatarUrl']=$member['avatarUrl'];
                $pz_level=Db::name('pz_level')->find($v['pz_level_id']);
                $_list[$k]['pz_level_name']=$pz_level['name'];
                $_list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
                $_list[$k]['status_time']=date('Y-m-d H:i:s',$v['status_time']);
            }
            $total = Db::name('fx')
                ->where($business_where)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $area_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();
        foreach ($area_list as $k => $v) {
            $proviec[$v['id']] = $v['name'];
        }
        $this->assign('proviec', $proviec);
        return $this->fetch();
    }

    //增加
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['row']['status']=1;
            $data['row']['create_time'] = time();
            $data['row']['status_time'] = time();
            $stmt = Db::name('fx')->insertGetId($data['row']);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        } else {
            $area_list = Db::name('area')->where([
                ['id', '<>', 0],
                ['pid', '=', 0]
            ])->select();
            foreach ($area_list as $k => $v) {
                $proviec[$v['id']] = $v['name'];
            }
            $plevel_list = Db::name('pz_level')->where([
                ['is_del', '=', 0]
            ])->select();
            $this->assign([
                'proviec'=>$area_list,
                'plevel_list'=>$plevel_list
            ]);
            return $this->fetch();
        }
    }


    public function edit()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data = Db::name('fx')
                ->update($data['row']);
            $this->success('操作成功');
        }
        $data = Db::name('fx')->find($id);
        $proviec_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();
        $provice = Db::name('area')->where(['name' => $data['province']])->find();
        $city_list = Db::name('area')->where([
            'pid' => $provice['id']
        ])->select();
        $city = Db::name('area')->where(['name' => $data['city']])->find();
        $area_list = Db::name('area')->where([
            'pid' => $city['id']
        ])->select();
        if ($area_list[0]['name'] == "市辖区") {
            $area_list2 = Db::name('area')->where([
                'pid' => $area_list[0]['id']
            ])->select();
            foreach ($area_list2 as $k => $v) {
                $area_list[] = $v;
            }
            unset($area_list[0]);
        }
        usort($area_list, function ($a, $b) {
            return $a['id'] - $b['id'];
        });

        $plevel_list = Db::name('pz_level')->where([
            ['is_del', '=', 0]
        ])->select();

        $this->assign([
            'data' => $data,
            'city_list' => $city_list,
            'proviec' => $proviec_list,
            'area_list' => $area_list,
            'plevel_list'=>$plevel_list
        ]);
        return $this->fetch();
    }

    public function del()
    {
        $ids = $this->request->param('ids/a', null);
        try {
            //软删除
            foreach ($ids as $id) {
                Db::name('fx')->where('id', $id)->update([
                    'is_del'=>1
                ]);
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }

    public function getc_cate()
    {
        $id = input('id');
        $detail = Db::name('area')->where('name', $id)->find();
        $query = db('area')->where('pid', $detail['id'])->select();
        if ($query[0]['name'] == "市辖区") {
            $area_list2 = Db::name('area')->where([
                'pid' => $query[0]['id']
            ])->select();
            foreach ($area_list2 as $k => $v) {
                $query[] = $v;
            }
            unset($query[0]);
        }
        usort($query, function ($a, $b) {
            return $a['id'] - $b['id'];
        });

        return json(['code' => 200, 'data' => $query, 'msg' => '获取成功']);
    }

    public function getc_cate2()
    {
        $id = input('id');
        $query = db('area')->where('pid', $id)->select();
        return json(['code' => 200, 'data' => $query, 'msg' => '获取成功']);
    }

}